package com.dxj.response;

import com.dxj.sysEnum.SysHttpStatus;

import java.io.Serializable;

/**
 * @author dxj
 * @Description 自定义全局返回
 * @date 2024/7/19 15:36
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private T data;

    public R() {
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> success() {
        return new R<>(SysHttpStatus.SUCCESS.getCode(), SysHttpStatus.SUCCESS.getMessage(), null);
    }

    public static <T> R<T> success(String msg) {
        return new R<>(SysHttpStatus.SUCCESS.getCode(), msg, null);
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> R<T> success(String msg, T data) {
        return new R<>(SysHttpStatus.SUCCESS.getCode(), msg, data);
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(SysHttpStatus.FAIL.getCode(), msg, null);
    }

    public static <T> R<T> fail() {
        return new R<>(SysHttpStatus.FAIL.getCode(), SysHttpStatus.FAIL.getMessage(), null);
    }


    public static <T> R<T> fail(SysHttpStatus status) {
        return new R<>(status.getCode(), status.getMessage(), null);
    }

    public static <T> R<T> fail(SysHttpStatus status, T data) {
        return new R<>(status.getCode(), status.getMessage(), data);
    }
}
